/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.ssau.lib;

import org.jdom.Element;
import ru.ssau.entities.Message;
import ru.ssau.entities.User;
import ru.ssau.entities.Thread;
import ru.ssau.utils.XMLHelper;

/**
 *
 * @author Дима
 */
public class Request implements java.io.Serializable, XMLizable {

    public static final int USER = 1;
    public static final int MESSAGE = 2;
    public static final int THREAD = 4;
    public static final int ADD = 1;
    public static final int DELETE = 2;
    public static final int REPLACE = 4;
    public static final int GET = 8;
    public static final int CHANGE = 16;
    public static final int STATE_NOTIFICATION = 32;
    public static final int SET = 64;
    private int action;
    private Integer id;
    private Object entity;
    private int entityType;

    public Element toXMLElement() {
        Element root = new Element("Request");
        XMLHelper xhelp = new XMLHelper();
        xhelp.addElement(root, "action", action);
        if (id != null) {
            xhelp.addElement(root, "id", id);
        }
        if (entity != null) {
            xhelp.addElement(root, ((XMLizable) entity).toXMLElement());
        }
        xhelp.addElement(root, "entityType", entityType);
        return root;
    }

    public void loadFromXMLElement(Element data) {
        Element temp = null;
        if ((temp = data.getChild("action")) != null) {
            setAction(Integer.valueOf(temp.getValue()));
        }
        if ((temp = data.getChild("id")) != null) {
            setId(Integer.valueOf(temp.getValue()));
        }
        int nEntityType = 0;
        if ((temp = data.getChild("entityType")) != null) {
            nEntityType = Integer.valueOf(temp.getValue());
        }
        setEntityType(nEntityType);
        Element entityElement = null;
        Object nEntity = null;
        if (nEntityType == Request.USER
                && (entityElement = data.getChild("User")) != null) {
            User user = new User();
            user.loadFromXMLElement(entityElement);
            nEntity = (Object) user;
        } else if (nEntityType == Request.THREAD
                && (entityElement = data.getChild("Thread")) != null) {
            Thread thread = new Thread();
            thread.loadFromXMLElement(entityElement);
            nEntity = (Object) thread;
        } else if (nEntityType == Request.MESSAGE
                && (entityElement = data.getChild("Message")) != null) {
            Message message = new Message();
            message.loadFromXMLElement(entityElement);
            nEntity = (Object) message;
        }
        setEntity(nEntity);
    }

    public Request() {
    }

    public Request(int action) {
        this.action = action;
    }

    public Request(int entityType, int action) {
        this.entityType = entityType;
        this.action = action;
    }

    public Request(int entityType, int action, Object entity) {
        this(entityType, action);
        this.entity = entity;
    }

    public Request(int entityType, int action, int id) {
        this(entityType, action);
        this.id = id;
    }

    /*Getters and setters*/
    public int getAction() {
        return action;
    }

    public void setAction(int action) {
        this.action = action;
    }

    public Object getEntity() {
        return entity;
    }

    public void setEntity(Object entity) {
        this.entity = entity;
    }

    public int getEntityType() {
        return entityType;
    }

    public void setEntityType(int entityType) {
        this.entityType = entityType;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}
